using Admin.Application.Common;
using Admin.Application.Dto;
using Admin.Application.Dto.Permission;
using Admin.Application.Repositories;
using Admin.Domain.Entities;
using Admin.Domain.Repositories;

namespace Admin.Application.Services;

public class PermissionService(IRepository<Permission> permissionRepository,ISystemLogRepository logService) : IPermissionService{
    private readonly IRepository<Permission> _permissionRepository = permissionRepository;
    private readonly ISystemLogRepository _logService = logService;

    public async Task<Result<string>> AddPermissionAsync(PermissionDto permissionDto)
    {
        var permission = Permission.CreatePermission(permissionDto.PermissionName);
        if (permission == null)
        {
            await _logService.AddAsync($"权限创建失败：名称为 {permissionDto.PermissionName}");
            return Result<string>.Fail("权限创建失败");
        }

        await _permissionRepository.AddAsync(permission);
        await _logService.AddAsync($"权限创建成功：{permissionDto.PermissionName}");
        return Result<string>.Success("权限创建成功");
    }

    public async Task<Result<string>> DeletePermissionAsync(Guid permissionId)
    {
        var permission = await _permissionRepository.GetByIdAsync(permissionId);
        if (permission == null)
        {
            await _logService.AddAsync($"删除权限失败：权限不存在 - {permissionId}");
            return Result<string>.Fail("权限不存在");
        }

        var isPermissionUpdated = permission.DeletePermission();
        if (!isPermissionUpdated)
        {
            await _logService.AddAsync($"删除权限失败：删除失败 - {permissionId}");
            return Result<string>.Fail("权限删除失败");
        }

        await _permissionRepository.UpdateAsync(permission);
        await _logService.AddAsync($"删除权限成功：{permission.PermissionName}");
        return Result<string>.Success("权限删除成功");
    }

    public async Task<Result<List<ResPermissionDto>>> GetPagedPermissionAsync(int pageIndex, int pageSize)
    {
        if (pageIndex < 0 || pageSize < 0)
        {
            await _logService.AddAsync($"分页查询权限失败：无效的页码或数据条数 - 页码：{pageIndex}，条数：{pageSize}");
            return Result<List<ResPermissionDto>>.Fail("无效的页码或数据条数");
        }

        var permissionList = await _permissionRepository.GetPagedAsync(pageIndex, pageSize);

        var resPermissionDtoTasks = permissionList.Select(p =>
            new ResPermissionDto(
                p.Id,
                p.PermissionName,
                p.IsActive,
                p.Description
            )
        );

        await _logService.AddAsync($"分页查询权限成功：页码 {pageIndex}，条数 {pageSize}");
        return Result<List<ResPermissionDto>>.Success([.. resPermissionDtoTasks]);
    }

    public async Task<Result<ResPermissionDto?>> GetPermissionByIdAsync(Guid permissionId)
    {
        var permission = await _permissionRepository.GetByIdAsync(permissionId);
        if (permission == null)
        {
            await _logService.AddAsync($"查询权限失败：权限不存在 - {permissionId}");
            return Result<ResPermissionDto?>.Fail("权限不存在");
        }

        var resPermissionDto = new ResPermissionDto(
            permission.Id,
            permission.PermissionName,
            permission.IsActive,
            permission.Description
        );

        await _logService.AddAsync($"查询权限成功：{permission.PermissionName}");
        return Result<ResPermissionDto?>.Success(resPermissionDto);
    }

    public async Task<Result<string>> UpdateIsActive(Guid permissionId, ActiveDto activeDto)
    {
        var permission = await _permissionRepository.GetByIdAsync(permissionId);
        if (permission == null)
        {
            await _logService.AddAsync($"更新权限启用状态失败：权限不存在 - {permissionId}");
            return Result<string>.Fail("权限不存在");
        }

        bool isPermissionUpdated;
        if (activeDto.IsActive)
        {
            isPermissionUpdated = permission.EnablePermission();
        }
        else
        {
            isPermissionUpdated = permission.DisablePermission();
        }

        if (!isPermissionUpdated)
        {
            await _logService.AddAsync($"更新权限启用状态失败：更新失败 - {permissionId}");
            return Result<string>.Fail("权限启用状态更新失败");
        }

        await _permissionRepository.UpdateAsync(permission);
        await _logService.AddAsync($"更新权限启用状态成功：{permission.PermissionName}");
        return Result<string>.Success("权限启用状态更新成功");
    }

    public async Task<Result<string>> UpdatePermissionAsync(Guid permissionId, PermissionDto permissionDto)
    {
        var permission = await _permissionRepository.GetByIdAsync(permissionId);
        if (permission == null)
        {
            await _logService.AddAsync($"更新权限信息失败：权限不存在 - {permissionId}");
            return Result<string>.Fail("权限不存在");
        }

        bool isPermissionUpdated = permission.UpdatPermissionInfo(permissionDto.PermissionName, permissionDto.Description);
        if (!isPermissionUpdated)
        {
            await _logService.AddAsync($"更新权限信息失败：更新失败 - {permissionId}");
            return Result<string>.Fail("权限信息更新失败");
        }

        await _permissionRepository.UpdateAsync(permission);
        await _logService.AddAsync($"更新权限信息成功：{permission.PermissionName}");
        return Result<string>.Success("权限信息更新成功");
    }
}